package cn.tedu.hongbei.pms.service.impl;

import cn.tedu.commons.ex.ServiceException;
import cn.tedu.commons.web.ServiceCode;
import cn.tedu.hongbei.pms.mapper.AttributeMapper;
import cn.tedu.hongbei.pms.mapper.AttributeTemplateMapper;
import cn.tedu.hongbei.pms.pojo.entity.Attribute;
import cn.tedu.hongbei.pms.pojo.param.AttributeAddNewParam;
import cn.tedu.hongbei.pms.pojo.vo.AttributeListItemVO;
import cn.tedu.hongbei.pms.pojo.vo.AttributeStandardVO;
import cn.tedu.hongbei.pms.service.IAttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AttributeServiceImpl implements IAttributeService {
    @Autowired
    AttributeMapper attributeMapper;

    @Autowired
    AttributeTemplateMapper attributeTemplateMapper;

    @Override
    public void addNew(AttributeAddNewParam attributeAddNewParam) {
        log.debug("开始处理【添加属性】的业务，参数：{}", attributeAddNewParam);
        // 调用Mapper对象的int countByName(String name)方法统计此名称的属性的数量
        String name = attributeAddNewParam.getName();
        Long templateId = attributeAddNewParam.getTemplateId();
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).eq("template_id", templateId);
        int countByName = attributeMapper.selectCount(queryWrapper);
        log.debug("尝试在属性模板【{}】中添加属性【{}】，在数据库中此名称的属性数量为：{}", templateId, name, countByName);
        // 判断统计结果是否大于0
        if (countByName > 0) {
            // 是：属性名称已经存在，抛出RuntimeException异常
            String message = "添加属性失败！此属性模板中已经存在名称为【" + name + "】的属性！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 检查属性模板是否存在
        {
            Object queryResult = attributeTemplateMapper.getStandardById(templateId);
            if (queryResult == null) {
                String message = "添加属性失败！属性模板不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        // 创建对象
        Attribute attribute = new Attribute();
        // 复制属性
        BeanUtils.copyProperties(attributeAddNewParam, attribute);
        // 插入数据
        log.debug("即将向数据库中插入数据：{}", attribute);
        attribute.setIsAllowCustomize(0);//是否允许自定义，始终为否
        attribute.setGmtCreate(LocalDateTime.now());
        attribute.setGmtModified(LocalDateTime.now());
        int rows = attributeMapper.insert(attribute);
        if (rows != 1) {
            String message = "添加属性失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    //根据属性模板Id查询所有属性列表
    @Override
    public List<AttributeListItemVO> list(Long templateId) {
        return attributeMapper.listByTemplateId(templateId);
    }

    //根据
    @Override
    public AttributeStandardVO getStandardById(Long id) {
        return null;
    }
}
